﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using SQLServerHealthCheck.Model;
using System.Windows.Threading;
using AG.Utilities;

namespace SQLServerOptimizer.PL.WPF
{
    class RunScheduled :DispatcherObject
    {

        public ObservableCollection<SQLServerInstance> SQLServerInstances = new ObservableCollection<SQLServerInstance>();
        public ObservableCollection<SQLServerInstance> ConnectedSQLServerInstances = new ObservableCollection<SQLServerInstance>();
        public ObservableCollection<IssueServer> issueServers = new ObservableCollection<IssueServer>();
        public ObservableCollection<DatabaseTrace> TraceSQLInstances = new ObservableCollection<DatabaseTrace>();
        ConnectionCheckWorker _statusworker;
        DataCollectionWorker worker;
        private DispatcherTimer TraceStatusCheckTimer = new DispatcherTimer(DispatcherPriority.Normal);
        HealthCheckDataContext dc;

        App shut;
        public void scan(App app)
        {
            try{
            dc = new HealthCheckDataContext(System.Configuration.ConfigurationManager.AppSettings["ConnectionStringSSHCDatabase"].ToString());
            shut = app;

            var instances = from a in dc.ScheduledServices
                                             select a;
            start(instances.ToList());
                }
            catch(Exception e)
            {
                Logger.Error(e);
            }

        }

        private void start(List<ScheduledService> instances)
        {
            try{

            foreach (var item in instances)
            {
                SQLServerInstances.Add(new SQLServerInstance { Authentication = item.Authentication, InstanceName = item.Name, Login = item.Login, Password = item.Password, Port = item.Port.ToString() });

            }
            ConnectedSQLServerInstances = SQLServerInstances;
            worker = new DataCollectionWorker(this, ConnectedSQLServerInstances);
            worker.ProccessCompleted += new ProccesCompleted(worker_ProccessCompleted);
            _statusworker = new ConnectionCheckWorker(this, this.SQLServerInstances);
            _statusworker.StatusCheckProccessCompleted += new StatusCheckProccesCompleted(_statusworker_StatusCheckProccessCompleted);
            _statusworker.Start();
                }
            catch(Exception e)
            {
                Logger.Error(e);
            }
        }
        private void _statusworker_StatusCheckProccessCompleted()
        {
            try{
            if (this.ConnectedSQLServerInstances.Count > 0)
            {

                worker.Start();

            }
                }
            catch(Exception e)
            {
                Logger.Error(e);
            }
        }
        private void worker_ProccessCompleted()
        {

            try
            {
                var mail = from em in dc.Emails
                           select em;
                foreach (var email in mail)
                {
                    new MailReport().mail(email.From, email.To, email.Username, email.Password, email.Host, email.Port,  "issuereport" + DateTime.Now.Date.ToShortDateString());

                }
              //  new MailReport().mail("bram.crombach@gmail.com", "bram.crombach@gmail.com", "sqlserveroptimizer@gmail.com", "sqlserveroptimizerpassword", "smtp.gmail.com", "587", "testmail");
            }
            catch(Exception e)
            {
                Logger.Error(e);
            }
                shut.Shutdown();




        }
    }
}
